package com.neu.cloudFactory.controller;

import com.neu.cloudFactory.PO.ProductMsg;
import com.neu.cloudFactory.PO.ProductType;
import com.neu.cloudFactory.VO.ProductMsgVO;
import com.neu.cloudFactory.VO.ProductTypeVO;
import com.neu.cloudFactory.service.ProductService;
import com.neu.cloudFactory.utils.result.Result;
import com.neu.cloudFactory.utils.result.ResultFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/product")
public class ProductController {
    final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    //    新增产品
    @CrossOrigin
    @PostMapping(value = "/addProduct")
    @ResponseBody
    public Result addProduct(@RequestBody ProductMsgVO requestProductVO){
        ProductMsg productMsg = productService.changeType(requestProductVO);
        if(productService.isExist(productMsg)){
            return ResultFactory.buildFailResult("已添加过该产品");
        }else{
            productMsg.setCode(productService.generateCode());
            productService.saveProduct(productMsg);
            return ResultFactory.buildSuccessResult(productService.addIdToPro(productMsg),"添加成功");
        }
    }

    //列出所有产品
    @CrossOrigin
    @GetMapping(value = "/getAllProduct")
    @ResponseBody
    public Result getAllProduct(){
        return ResultFactory.buildSuccessResult(productService.findAllProduct());
    }
    //列出所有产品种类
    @CrossOrigin
    @GetMapping(value = "/getAllProductType")
    @ResponseBody
    public Result getAllProductType(){
        System.out.println(productService.findAllProductType().toString());
        return ResultFactory.buildSuccessResult(productService.findAllProductType());
    }
//    新增产品种类
    @CrossOrigin
    @PostMapping(value = "/addProductType")
    @ResponseBody
    public Result addProductType(@RequestBody ProductType requestProductType){
        if(null!=productService.findProductTypeByName(requestProductType.getName())){
            return ResultFactory.buildFailResult("已添加过该种类");
        }else{
            productService.saveProductType(requestProductType);
            ProductTypeVO productTypeVO = new ProductTypeVO(requestProductType);
            return ResultFactory.buildSuccessResult(productTypeVO,"添加成功");
        }
    }

//    修改产品
    @CrossOrigin
    @PostMapping(value = "/modifyProduct")
    @ResponseBody
    public Result modifyProduct(@RequestBody ProductMsgVO requestProductMsgVO){
        ProductMsg productMsg = productService.changeType(requestProductMsgVO);
        //修改的时候需要传入id
        productMsg.setId(Integer.parseInt(requestProductMsgVO.getId()));
        productService.saveProduct(productMsg);
        return ResultFactory.buildSuccessResult("修改成功");
    }

//    修改产品种类
    @CrossOrigin
    @PostMapping(value = "/modifyProductType")
    @ResponseBody
    public Result modifyProductType(@RequestBody ProductType requestProductType){
            productService.saveProductType(requestProductType);
            return ResultFactory.buildSuccessResult("修改成功");
}
//    删除产品
    @CrossOrigin
    @PostMapping(value = "/deleteProductMsg")
    @ResponseBody
    public Result deleteProductMsg(@RequestBody ProductMsgVO requestProductMsgVO){
        ProductMsg productMsg = productService.changeType(requestProductMsgVO);
        //删除的时候需要传入id
        productMsg.setId(Integer.parseInt(requestProductMsgVO.getId()));
        productService.deleteProduct(productMsg);
        return ResultFactory.buildSuccessResult("删除成功");
    }
//    删除产品种类
    @CrossOrigin
    @PostMapping(value = "/deleteProductType")
    @ResponseBody
    public Result deleteProductType(@RequestBody ProductType requestProductType){
        if(!productService.isOccupied(requestProductType)){
            productService.deleteProductType(requestProductType);
            return ResultFactory.buildSuccessResult("删除成功");
        }else{
            return ResultFactory.buildFailResult("类别被占用，不可删除");
        }
    }



}
